package basic;

/*
 * Author: JacksonCoder
 * FileName: test
 * Version: 1.0.0
 * Date: 2024/12/17
 */

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
//import org.apache.commons.lang3.ArrayUtils;

public class TestDemo {
    public static void main(String[] args) {
        String s = "abc";
        char c = s.charAt(0);
        System.out.println(c);

        for (char c1 : s.toCharArray()) {
            System.out.print(c1 + "\t");
        }
        System.out.println("");

        String[] words = {"abc", "def", "ghi"};
        for (String word : words) {
            System.out.println(word);
        }
        printArray();
        sortArray();
        toArray();
        ArrayToStringList();
        StringOperate();
        StringBufferExample();
    }

    public static void printArray() {
        Integer[] arr = {1, 2, 3, 4, 5};
        for (int i : Arrays.asList(arr)) {
            System.out.println(i);
        }
    }

    public static void sortArray() {
        int[] arr = {5, 4, 3, 2, 1, 9, 3, 4, 8};
        Arrays.sort(arr);
        // print arr
        for (int i : arr) {
            System.out.println(i);
        }
        List<Integer> arrayList = Arrays.asList(5, 4, 3, 2, 1, 9, 3, 4, 8);
        System.out.println(arrayList);

    }

    public static void toArray() {
        int[] arr = {5, 4, 3, 2, 1, 9, 7, 6, 8};
        Arrays.sort(arr);

        // 1
        List<Integer> arrayList = new ArrayList<>();
        for (int i : arr) {
            arrayList.add(i);
        }
        System.out.println(arrayList);

        Integer[] arr2 = arrayList.toArray(new Integer[0]);
        System.out.println(arr2.length);

        // 2
        List<Integer> arrayList2 = Arrays.stream(arr).boxed().collect(Collectors.toList());
        ArrayList<Integer> ArrayList = new ArrayList<>(arrayList2);
        System.out.println(ArrayList);

        // 3
        ArrayList<Integer> arrayList3 = new ArrayList<>();
        Collections.addAll(arrayList3, IntStream.of(arr).boxed().toArray(Integer[]::new));
        System.out.println(arrayList3);

        // 4   Apache Commons Lang库
//        Integer[] integerArray = ArrayUtils.toObject(arr);
//        ArrayList<Integer> arrayList4 = new ArrayList<>(Arrays.asList(integerArray));

    }

    public static void ArrayToStringList() {
        List<List<String>> StringList = new ArrayList<>();
        Random random = new Random();
        for (int n = 0; n < 5; n++) {
            List<String> list = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                char c = (char) (random.nextInt(26) + 'A');
                list.add(String.valueOf(c));
            }
            StringList.add(list);
        }
        System.out.println(StringList);
    }

    public static void StringOperate() {
        // 创建一个StringBuilder对象
        StringBuilder sb = new StringBuilder("Hello");
        System.out.println(sb);
        // 追加字符串
        sb.append(" World");
        System.out.println(sb);
        // 插入字符串
        sb.insert(5, ",");
        System.out.println(sb);
        // 删除字符
        sb.deleteCharAt(5);
        System.out.println(sb);
        // 替换字符串
        sb.replace(6, 11, "Java");
        System.out.println(sb);
        // 反转字符串
        sb.reverse();
        // 转换为字符串
        String result = sb.toString();
        System.out.println(result + "\n"); // 输出: "avaj ,olleH"
    }

    public static void StringBufferExample() {
        // 创建一个StringBuffer对象
        StringBuffer sb = new StringBuffer("Hello");
        // 同步代码块，确保线程安全
        synchronized (sb) {
            // 追加字符串
            sb.append(" World");
            System.out.println(sb);
            // 插入字符串
            sb.insert(5, ",");
            System.out.println(sb);
            // 删除字符
            sb.deleteCharAt(5);
            System.out.println(sb);
            // 替换字符串
            sb.replace(6, 11, "Java");
            System.out.println(sb);
            // 反转字符串
            sb.reverse();
        }
        // 转换为字符串
        String result = sb.toString();
        System.out.println(result); // 输出: "dlava ,olleH"
    }

}
